﻿package com.evidencer.java.net;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class TCPBrowserTest {

	/**
	 * @param args
	 * @throws IOException 
	 * @throws UnknownHostException 
	 */
	public static void main(String[] args) throws UnknownHostException, IOException {
		Socket s=new Socket("127.0.0.1",8088);
		
		BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
		bw.write("GET / HTTP/1.1");
		bw.newLine();
		bw.write("Accept: */*");
		bw.newLine();
		bw.write("Accept-Language: ja");
		bw.newLine();
		bw.write("User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; OfficeLiveConnector.1.3; OfficeLivePatch.0.0; Avant Browser)");
		bw.newLine();
		bw.write("Accept-Encoding: gzip, deflate");
		bw.newLine();
		bw.write("Host: 127.0.0.1:8088");
		bw.newLine();
		bw.write("Connection: Keep-Alive");
		bw.newLine();
		bw.newLine();
		
		bw.flush();
//		PrintWriter out = new PrintWriter(s.getOutputStream(),true);
//		out.println("GET /myweb/1.html HTTP/1.1");
//		out.println("Accept: */*");
//		out.println("Host: 127.0.0.1:8088");
//		out.println("Connection: close");
//		out.println();
//		out.println();
		
		System.out.println("请求成功！");
		
		InputStream in = s.getInputStream();
		
		byte[] buf = new byte[1024];
		int len = 0;
		while((len=in.read(buf))!=-1){
			String str =new String(buf,0,len);
			System.out.println(str);
		}
		
		
		
		
		
//		BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
//		String line="";
//		while((line=br.readLine())!=null&&!"".equals(line)){
//			System.out.println(line);
//		}
//		
		s.close();

	}

}
